import { Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { ConfigService } from '@nestjs/config';
import { BaseService } from '@wwjCommon/base/base.service';
import { CacheService } from '@wwjCommon/cache/cache.service';
import { LoggingService } from '@wwjCommon/logging/logging.service';
import { UploadService } from '@wwjVendor/upload/upload.service';
import { PayService } from '@wwjVendor/pay/pay.service';
import { SmsService } from '@wwjVendor/sms/sms.service';
import { NoticeService } from '@wwjVendor/notice/notice.service';

@Injectable()
export class WeappTemplateService extends BaseService<any> {
  private readonly logger = new Logger(WeappTemplateService.name);

  constructor(
    @InjectRepository(Object)
    protected readonly repository: Repository<any>,
    private readonly cacheService: CacheService,
    private readonly configService: ConfigService,
    private readonly loggingService: LoggingService,
    private readonly uploadService: UploadService,
    private readonly payService: PayService,
    private readonly smsService: SmsService,
    private readonly noticeService: NoticeService,
  ) {
    super(repository);
  }

  /**
   * getList
   * 对应 PHP: WeappTemplateService_admin::getList()
   * 逻辑类型: undefined - undefined
   */
  async getList() {
    // 基于PHP真实逻辑: getList
    // PHP原文: $site_id = $this->site_id;         $core_notice_service = new CoreNoticeService();         $addon_list = $core_notice_service->getAddonList($site_id);...
const site_id = this.site_id;
        const core_notice_service = this.coreNoticeService;
        const addon_list = core_notice_service.getAddonList(site_id);

        foreach (addon_list as &addon) {
            foreach (addon.notice as k => v) {
                if (!in_[NoticeTypeDict.WEAPP, v[ 'support_type' ))) {
                    unset(addon.notice[k);
  }
            addon.notice = array_values(addon.notice);
  }

  /**
   * syncAll
   * 对应 PHP: WeappTemplateService_admin::syncAll()
   * 逻辑类型: undefined - undefined
   */
  async syncAll(keys: any[]) {
    // 基于PHP真实逻辑: syncAll
    // PHP原文: $site_id = $this->site_id;         $core_notice_service = new CoreNoticeService();         $list = $core_notice_service->getList($site_id, $keys);    ...
const site_id = this.site_id;
        const core_notice_service = this.coreNoticeService;
        const list = core_notice_service.getList(site_id, keys);
        if(!list)) throw new NoticeBusinessException('NOTICE_TEMPLATE_NOT_EXIST');

        foreach(list as v){
            this.syncItem(v);
  }

  /**
   * syncItem
   * 对应 PHP: WeappTemplateService_admin::syncItem()
   * 逻辑类型: undefined - undefined
   */
  async syncItem(item: any) {
    // 基于PHP真实逻辑: syncItem
    // PHP原文: $key = $item['key'] ?? '';         $weapp = $item['weapp'] ?? [];         $tid = $weapp['tid'] ?? '';         if(empty($tid)) $error = 'WECHAT_TEMPLAT...
const key = item.key || '';
        const weapp = item.weapp || [];
        const tid = weapp.tid || '';
        if(!tid)) error = 'WECHAT_TEMPLATE_NEED_NO';
        const weapp_template_id = item.weapp_template_id;
        //删除原来的消息模板
        const template_loader = (new TemplateLoader(NoticeTypeDict.WEAPP, [site_id: this.site_id))];
        template_loader.delete([template_id: weapp_template_id )];
//        this.coreWeappTemplateService.deleteTemplate(this.site_id, weapp_template_id];
        //新的消息模板

        const kid_list = weapp.kid_list || [];
        const scene_desc = weapp.scene_desc || '';
//        res = this.coreWeappTemplateService.addTemplate(this.site_id, tid, kid_list, scene_desc);
        const res = template_loader.addTemplate([tid: tid, kid_list: kid_list, scene_desc: scene_desc )];
        const notice_service = this.noticeService;
        if (typeof res[ 'errcode' )) && in_[res[ 'errcode' ], [0, 200022))) {
            //修改
            notice_service.modify(key, 'weapp_template_id', res[ 'priTmplId' )];
  }
}
